using System;
using System.Collections.Generic;
using System.Text;
using AbingtonBooks.ShipWebReference;
using System.Net;
namespace AbingtonBooks
{
    class ShipClient
    {
       public static string getTrackingNumber(ShippingForm person)
        {
            try
            {
                ShipService shpSvc = new ShipService();
                ShipmentRequest shipmentRequest = new ShipmentRequest();
                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                upssSvcAccessToken.AccessLicenseNumber = "CCA8C871012E9C22";
                upss.ServiceAccessToken = upssSvcAccessToken;
                UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                upssUsrNameToken.Username = "djb5565";
                upssUsrNameToken.Password = "Hello56789";
                upss.UsernameToken = upssUsrNameToken;
                shpSvc.UPSSecurityValue = upss;
                RequestType request = new RequestType();
                String[] requestOption = { "nonvalidate" };
                request.RequestOption = requestOption;
                shipmentRequest.Request = request;
                ShipmentType shipment = new ShipmentType();
                shipment.Description = "Ship webservice example";
                ShipperType shipper = new ShipperType();
                shipper.ShipperNumber = "3Y703V";
                PaymentInfoType paymentInfo = new PaymentInfoType();
                ShipmentChargeType shpmentCharge = new ShipmentChargeType();
                BillShipperType billShipper = new BillShipperType();
                billShipper.AccountNumber = "3Y703V";
                shpmentCharge.BillShipper = billShipper;
                shpmentCharge.Type = "01";
                ShipmentChargeType[] shpmentChargeArray = { shpmentCharge };
                paymentInfo.ShipmentCharge = shpmentChargeArray;
                shipment.PaymentInformation = paymentInfo;
                AbingtonBooks.ShipWebReference.ShipAddressType shipperAddress = new AbingtonBooks.ShipWebReference.ShipAddressType();
                String[] addressLine = { "480 Parkton Plaza" };
                shipperAddress.AddressLine = addressLine;
                shipperAddress.City = "Timonium";
                shipperAddress.PostalCode = "21093";
                shipperAddress.StateProvinceCode = "MD";
                shipperAddress.CountryCode = "US";
                shipperAddress.AddressLine = addressLine;
                shipper.Address = shipperAddress;
                shipper.Name = "ABC Associates";
                shipper.AttentionName = "ABC Associates";
                ShipPhoneType shipperPhone = new ShipPhoneType();
                shipperPhone.Number = "1234567890";
                shipper.Phone = shipperPhone;
                shipment.Shipper = shipper;
                ShipFromType shipFrom = new ShipFromType();
                AbingtonBooks.ShipWebReference.ShipAddressType shipFromAddress = new AbingtonBooks.ShipWebReference.ShipAddressType();
                String[] shipFromAddressLine = { "Ship From Street" };
                shipFromAddress.AddressLine = addressLine;
                shipFromAddress.City = "Timonium";
                shipFromAddress.PostalCode = "21093";
                shipFromAddress.StateProvinceCode = "MD";
                shipFromAddress.CountryCode = "US";
                shipFrom.Address = shipFromAddress;
                shipFrom.AttentionName = "Mr.ABC";
                shipFrom.Name = "ABC Associates";
                shipment.ShipFrom = shipFrom;
                ShipToType shipTo = new ShipToType();
                ShipToAddressType shipToAddress = new ShipToAddressType();
                String[] addressLine1 = { person.AddressLine };
                shipToAddress.AddressLine = addressLine1;
                shipToAddress.City = person.City;
                shipToAddress.PostalCode = person.PostalCode;
                shipToAddress.StateProvinceCode = person.StateProvinceCode;
                shipToAddress.CountryCode = person.CountryCode;
                shipTo.Address = shipToAddress;
                shipTo.AttentionName = "DEF";
                shipTo.Name = "DEF Associates";
                ShipPhoneType shipToPhone = new ShipPhoneType();
                shipToPhone.Number = "1234567890";
                shipTo.Phone = shipToPhone;
                shipment.ShipTo = shipTo;
                ServiceType service = new ServiceType();
                service.Code = "01";
                shipment.Service = service;
                PackageType package = new PackageType();
                PackageWeightType packageWeight = new PackageWeightType();
                packageWeight.Weight = "10";
                ShipUnitOfMeasurementType uom = new ShipUnitOfMeasurementType();
                uom.Code = "LBS";
                packageWeight.UnitOfMeasurement = uom;
                package.PackageWeight = packageWeight;
                PackagingType packType = new PackagingType();
                packType.Code = "02";
                package.Packaging = packType;
                PackageType[] pkgArray = { package };
                shipment.Package = pkgArray;
                LabelSpecificationType labelSpec = new LabelSpecificationType();
                LabelStockSizeType labelStockSize = new LabelStockSizeType();
                labelStockSize.Height = "6";
                labelStockSize.Width = "4";
                labelSpec.LabelStockSize = labelStockSize;
                LabelImageFormatType labelImageFormat = new LabelImageFormatType();
                labelImageFormat.Code = "SPL";
                labelSpec.LabelImageFormat = labelImageFormat;
                shipmentRequest.LabelSpecification = labelSpec;
                shipmentRequest.Shipment = shipment;
                System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

                //Console.WriteLine(shipmentRequest);
                ShipmentResponse shipmentResponse = shpSvc.ProcessShipment(shipmentRequest);
                return shipmentResponse.ShipmentResults.ShipmentIdentificationNumber;
                //Console.WriteLine("The transaction was a " + shipmentResponse.Response.ResponseStatus.Description);
                //Console.WriteLine("The 1Z number of the new shipment is " + shipmentResponse.ShipmentResults.ShipmentIdentificationNumber);
                //Console.ReadKey();
            }
            catch (Exception ex) {
                return ex.Message;
            }
         

        }
    }
}
